package cn.soulic.tools.redis.migration.handler;

import cn.soulic.tools.redis.migration.config.RedisMigrationProperties;
import io.lettuce.core.MapScanCursor;
import io.lettuce.core.ScanArgs;
import io.lettuce.core.ScanCursor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Hash数据处理
 *
 * @author Soulic Yang
 * @since 1.0.0
 */
@Component(value = "hash")
public class HashHandler extends AbstractHandler {
    private final Logger logger = LoggerFactory.getLogger(HashHandler.class);

    @Resource
    private RedisMigrationProperties properties;

    @Override
    public void doHandle(String key) {
        ScanCursor hScanCursor = ScanCursor.INITIAL;
        ScanArgs matchesAll = ScanArgs.Builder.matches(MATCHER_ALL).limit(properties.getMaxCount());

        do {
            MapScanCursor<String, String> cursor = sourceCommands.hscan(key, hScanCursor, matchesAll);
            Map<String, String> map = cursor.getMap();
            targetCommands.hmset(key, map);
            hScanCursor = cursor;
        } while (!hScanCursor.isFinished());
    }
}
